package main

import (
	"fmt"
	"os"
	codecs "sanguo/codec/cs"
	"sanguo/node/client/dispatcher"
	"sanguo/node/client/login"
	cs_msg "sanguo/protocol/cs/message"

	"github.com/golang/protobuf/proto"
	"github.com/sniperHW/kendynet"
)

func main() {

	if len(os.Args) < 3 {
		fmt.Printf("usage selectRole userID addr\n")
		return
	}

	userID := os.Args[1]
	addr := os.Args[2]

	login.Login(userID, addr, func(session kendynet.StreamSession, d *dispatcher.Dispatcher, msg *codecs.Message, err error) {
		if nil != err {
			fmt.Println(err)
		} else {
			gameLoginToC := msg.GetData().(*cs_msg.GameLoginToC)
			fmt.Println("RoleList", gameLoginToC)
			if gameLoginToC.GetCode() == cs_msg.EnumType_OK {
				var roleIDMap map[int]uint64 = make(map[int]uint64)

				req := &cs_msg.RoleListToS{}
				session.Send(codecs.NewMessage(uint16(0), req))

				d.Register(&cs_msg.CreateTeamToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.CreateTeamToC)
					fmt.Println("CreateTeamToC", resp)
					//CreateTeam(session)

				})
				d.Register(&cs_msg.UpdateTeamToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.UpdateTeamToC)
					fmt.Println("UpdateTeamToC", resp)

				})
				d.Register(&cs_msg.TeamListToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.TeamListToC)
					fmt.Println("TeamListToC", resp)
					JoinTeam(session, resp)

				})
				d.Register(&cs_msg.JoinTeamToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.JoinTeamToC)
					fmt.Println("JoinTeamToC", resp)

				})
				d.Register(&cs_msg.ReplyJoinTeamToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.ReplyJoinTeamToC)
					fmt.Println("ReplyJoinTeamToC", resp)

				})
				d.Register(&cs_msg.ApplyToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.ApplyToC)
					fmt.Println("ApplyToC", resp)
					ReplyJoinTeam(session, resp)

				})
				d.Register(&cs_msg.LeaveTeamToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.LeaveTeamToC)
					fmt.Println("LeaveTeamToC", resp)
				})
				d.Register(&cs_msg.KickTeammateToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.KickTeammateToC)
					fmt.Println("KickTeammateToC", resp)
				})
				d.Register(&cs_msg.CallTeammateToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.CallTeammateToC)
					fmt.Println("CallTeammateToC", resp)
				})

				d.Register(&cs_msg.UpdateFightInviteToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.UpdateFightInviteToC)
					fmt.Println("UpdateFightInviteToC", resp)
					ReplyFight(session, resp)
				})
				d.Register(&cs_msg.MatchToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.MatchToC)
					fmt.Println("MatchToC", resp)
				})

				d.Register(&cs_msg.RoleListToC{}, false, func(args []interface{}) {
					resp := args[1].(*codecs.Message).GetData().(*cs_msg.RoleListToC)
					fmt.Println(resp.GetErrcode())
					for k, v := range resp.RoleList {
						fmt.Println(k, v)
						roleIDMap[k] = v.GetID()
						fmt.Println(k, roleIDMap[k])
					}

					if resp.GetErrcode() == cs_msg.EnumType_OK {
						req2 := &cs_msg.SelectRoleToS{
							ID: proto.Uint64(roleIDMap[0]),
						}
						session.Send(codecs.NewMessage(uint16(0), req2))
						d.Register(&cs_msg.SelectRoleToC{}, false, func(args []interface{}) {
							resp2 := args[1].(*codecs.Message).GetData().(*cs_msg.SelectRoleToC)
							fmt.Println("SelectRoleToC", resp2)
							if resp2.GetErrcode() == cs_msg.EnumType_OK {
								//for {
								fmt.Printf("1创建队伍 2申请队伍 3离开队伍 4踢人 5召集队员\n")
								fmt.Printf("6匹配申请 7匹配取消:")
								var k int
								fmt.Scan(&k)
								if k == 1 {
									CreateTeam(session)
								} else if k == 2 {
									TeamList(session)
								} else if k == 3 {
									LeaveTeam(session)
								} else if k == 4 {
									KickTeammate(session)
								} else if k == 5 {
									CallTeammate(session)
								} else if k == 6 {
									MatchRequest(session)
								} else if k == 7 {
									MatchCancel(session)
								}

								//}
							}
						})
					}

				})

			}
		}
	})

	sigStop := make(chan bool)
	_, _ = <-sigStop
}

func CreateTeam(session kendynet.StreamSession) {
	go func() {
		fmt.Printf("teamType:")
		var id int
		fmt.Scan(&id)

		req := &cs_msg.CreateTeamToS{
			TeamType: proto.Int32(int32(id)),
		}
		session.Send(codecs.NewMessage(uint16(0), req))
	}()
}

func TeamList(session kendynet.StreamSession) {
	req := &cs_msg.TeamListToS{
		FrontNo: proto.Int32(1),
		RearNo:  proto.Int32(10),
	}
	session.Send(codecs.NewMessage(uint16(0), req))
}

func JoinTeam(session kendynet.StreamSession, resp *cs_msg.TeamListToC) {
	go func() {
		teamList := map[int]uint64{}
		for k, v := range resp.GetTeams() {
			teamList[k] = v.GetTeamID()
			fmt.Println(k, v.GetTeamID())
		}

		fmt.Printf("TeamID:")
		var id int
		fmt.Scan(&id)

		req := &cs_msg.JoinTeamToS{
			TeamID: proto.Uint64(teamList[id]),
		}
		session.Send(codecs.NewMessage(uint16(0), req))
	}()
}

func ReplyJoinTeam(session kendynet.StreamSession, resp *cs_msg.ApplyToC) {
	go func() {
		fmt.Printf("IsAgreed:")
		var id int
		fmt.Scan(&id)

		req := &cs_msg.ReplyJoinTeamToS{
			RoleID: proto.Uint64(resp.GetRoleID()),
			UserID: proto.String(resp.GetUserID()),
		}

		if id == 1 {
			req.TeamCode = cs_msg.TeamEnum(cs_msg.TeamEnum_AGREED).Enum()
		} else {
			req.TeamCode = cs_msg.TeamEnum(cs_msg.TeamEnum_DISAGREE).Enum()
		}

		session.Send(codecs.NewMessage(uint16(0), req))
	}()
}

func LeaveTeam(session kendynet.StreamSession) {
	go func() {
		req := &cs_msg.LeaveTeamToS{}
		session.Send(codecs.NewMessage(uint16(0), req))
	}()
}

func KickTeammate(session kendynet.StreamSession) {
	go func() {
		fmt.Printf("RoleID:")
		var id int
		fmt.Scan(&id)

		req := &cs_msg.KickTeammateToS{
			RoleID: proto.Uint64(uint64(id)),
		}
		session.Send(codecs.NewMessage(uint16(0), req))
	}()
}

func CallTeammate(session kendynet.StreamSession) {
	go func() {
		req := &cs_msg.CallTeammateToS{}
		session.Send(codecs.NewMessage(uint16(0), req))
	}()
}

func ReplyFight(session kendynet.StreamSession, resp *cs_msg.UpdateFightInviteToC) {
	go func() {
		req := &cs_msg.ReplyFightInviteToS{
			Reply: cs_msg.FightInviteType(cs_msg.FightInviteType_Agree).Enum(),
		}

		session.Send(codecs.NewMessage(uint16(0), req))
	}()
}

func MatchRequest(session kendynet.StreamSession) {
	go func() {
		req := &cs_msg.MatchToS{
			MsgType: cs_msg.MatchType(cs_msg.MatchType_Request).Enum(),
		}
		session.Send(codecs.NewMessage(uint16(0), req))
	}()
}

func MatchCancel(session kendynet.StreamSession) {
	go func() {
		req := &cs_msg.MatchToS{
			MsgType: cs_msg.MatchType(cs_msg.MatchType_Cancel).Enum(),
		}
		session.Send(codecs.NewMessage(uint16(0), req))
	}()
}
